package com.session3;

import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class JListDemo extends JFrame implements ListSelectionListener{
	private JList listEmp;
	private Vector<String> vtrData;
	public JListDemo(){
		
	}
	public void createForm(){
		this.vtrData = new Vector<String>();
		this.vtrData.addElement("Pham van viet");
		this.vtrData.addElement("Nguyen Van An");
		this.vtrData.addElement("Pham Thi Thu");
		
		this.listEmp = new JList(this.vtrData);
		this.listEmp.setLayoutOrientation(JList.VERTICAL_WRAP);
		this.getContentPane().add(new JScrollPane(this.listEmp));
		
		this.listEmp.addListSelectionListener(this);
	}
	public void showForm(){
		this.setTitle("Demo JList");
		this.setSize(400,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	@Override
	public void valueChanged(ListSelectionEvent e) {
		if(e.getSource() == this.listEmp){
			if(e.getValueIsAdjusting() == true){
				String value = this.listEmp.getSelectedValue().toString();
				System.out.println(value);
			}
		}
	}
	public static void main(String[] args) {
		JListDemo d = new JListDemo();
		d.createForm();
		d.showForm();
	}
}
